package org.hj.modules.generatecode.service.impl;

import org.apache.shiro.SecurityUtils;
import org.hj.common.system.vo.LoginUser;
import org.hj.modules.generatecode.entity.ViewUiproperties;
import org.hj.modules.generatecode.mapper.ViewUipropertiesMapper;
import org.hj.modules.generatecode.service.IViewUipropertiesService;
import org.hj.modules.generatecode.util.DatabaseUtil;
import org.hj.modules.generatecode.vo.DatabaseVO;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: UI属性
 * @Author: gaobw
 * @Date: 2020-06-16
 * @Version: V1.0
 */
@Service
public class ViewUipropertiesServiceImpl extends ServiceImpl<ViewUipropertiesMapper, ViewUiproperties> implements IViewUipropertiesService {

    private DatabaseUtil databaseUtil;

    @Override
    public Object saveColumn(DatabaseVO databaseVO, int order) {
        doSaveColumn(null,null,databaseVO, order);

        return null;
    }

    @Override
    public Object saveColumn(String parentId, String type, DatabaseVO databaseVO, int order) {
        doSaveColumn(parentId,type,databaseVO, order);

        return null;
    }

    private void doSaveColumn(String parentId,String type,DatabaseVO databaseVO, int order) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        ViewUiproperties viewUiproperties = new ViewUiproperties();
        viewUiproperties.setUiFieldname(databaseVO.getColumnName());
        viewUiproperties.setFieldName(replaceUpperCase(databaseVO.getColumnName()));
        viewUiproperties.setUiFieldtype(databaseVO.getColumnType());
        viewUiproperties.setUiFieldcomment(databaseVO.getColumnComment());
        viewUiproperties.setUiTable(databaseVO.getTableName());
        viewUiproperties.setUiCreatetime(new Date());
        viewUiproperties.setUiCreateuser(sysUser.getId());
        viewUiproperties.setUiNotnull(databaseVO.getColumnIsNull());
        viewUiproperties.setUiApplist("false");
        viewUiproperties.setUiAppview("false");
        viewUiproperties.setUiOnlyread("false");
        viewUiproperties.setUiDefaultvalue("");
        viewUiproperties.setUiExpression("");
        viewUiproperties.setUiSelectfunc("");
        viewUiproperties.setUiUitype("0");


        viewUiproperties.setParentId(parentId);
        viewUiproperties.setIsDetailed(type);

        if ("DATETIME".equals(databaseVO.getColumnType())) {
            viewUiproperties.setUiUitype("7");//7表示的是时间框
        } else if ("DATE".equals(databaseVO.getColumnType())) {
            viewUiproperties.setUiUitype("2");//2表示的是日期框
        } else if ("DOUBLE".equals(databaseVO.getColumnType()) || "FLOAT".equals(databaseVO.getColumnType()) || "INT".equals(databaseVO.getColumnType())) {
            viewUiproperties.setUiUitype("4");//4表示的是数字框
        }
        viewUiproperties.setUiForm("false");
        viewUiproperties.setUiOrder(order);

        this.save(viewUiproperties);
        viewUiproperties = null;
        databaseVO = null;
    }

    public String replaceUpperCase(String str) {
        StringBuffer sb = new StringBuffer();
        sb.append(str);
        int count = sb.indexOf("_");
        while (count != 0) {
            int num = sb.indexOf("_", count);
            count = num + 1;
            if (num != -1) {
                char ss = sb.charAt(count);
                char ia = (char) (ss - 32);
                sb.replace(count, count + 1, ia + "");
            }
        }
        String result = sb.toString().replaceAll("_", "");
        return result;
    }

}
